package com.luhui.codereview.actions

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.fileChooser.FileChooser
import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.luhui.codereview.QrCodeIcon
import com.luhui.codereview.api.ApiManager
import com.luhui.codereview.dto.ScanQrCodeDto
import com.luhui.codereview.getMessage
import com.luhui.codereview.service.ProjectService
import com.luhui.codereview.token.LocalUserInfoHodler
import com.luhui.codereview.utils.RespBody
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.awt.EventQueue
import java.io.File

/**
 * 二维码扫描
 * @author  luhui
 * @date  2019/11/18 22:02
 * @version 1.0
 */
class QrCodeAction(val project:Project) : AnAction(getMessage("tab.refresh.action.qrCodeText"), getMessage("tab.refresh.action.qrCodeTitle"), QrCodeIcon) {

    override fun actionPerformed(p0: AnActionEvent?) {
        //读取用户选择的二维码，并发送给服务端，解析出二维码的内容

        val descriptor = FileChooserDescriptor(true, false, false, false, false, false)
        descriptor.title = getMessage("qrFrame.selectQr.title")
        descriptor.withFileFilter {
            it.path.endsWith(".jpg")
        }
        val virtualFile = FileChooser.chooseFile(descriptor, project, null)
        if (virtualFile != null) {
            val selectPath = virtualFile.path
            val selectFile = File(selectPath)
            GlobalScope.launch{
                val scanQrCodeDto = ApiManager.getApiInstance().scanQrCode(MultipartBody.Part.createFormData("file", selectFile.name,
                        RequestBody.create(MediaType.parse("multipart/form-data"), selectFile)),
                        LocalUserInfoHodler.localUserInfo?.id!!
                )
                if (scanQrCodeDto.success) {
                    val respBody = scanQrCodeDto.body
                    if (respBody.action == "JOIN_REVIEW_GROUP") {
                        val projectService = ProjectService.getInstance(project)
                        val projectInfo = projectService.getCurrentProjectInfo()!!
                        projectInfo.id = respBody.data
                        projectService.updateCurrentProjectInfo(projectInfo)
                        EventQueue.invokeLater {
                            Messages.showInfoMessage(getMessage("addToUserGroup.success.message"), getMessage("remind"))
                        }
                    }

                } else {
                    EventQueue.invokeLater {
                        Messages.showInfoMessage("加入失败，可能是您已经加入用户组", getMessage("remind"))
                    }
                }
            }
        }
    }

    override fun displayTextInToolbar(): Boolean {
        return true
    }
}